package org.wadael.waverobotfrmwrk.examples.advanced;

import org.wadael.waverobotfrmwrk.advanced.RobotWorker;
import org.wadael.waverobotfrmwrk.advanced.WithWorkersRobot;

import com.google.wave.api.Blip;
/**
 * Example of robot using workers
 * 
 * @author jbaton : wadael http://blog.wadael.org  http://twitter.com/wadael
 *
 */
@SuppressWarnings("serial")
public class MyWithWorkersRobot extends WithWorkersRobot {

	public MyWithWorkersRobot(){
		super();
		addRobotWorker( new CapitalizeWorker() );
		addRobotWorker( new LengthWorker() );
	}
	
	@Override
	public String getRobotIdentifier() {
		return "my";
	}

	@Override
	protected String getRobotSelfIntroduction() {
		return super.getRobotSelfIntroduction() + " Mr Waver, I am an advanced robot, what about you ?";
	}
	
	@Override
	protected String getDisplayName() {
		return "EssaiWorkers (" + super.getDisplayName() +")";
	}
	
}

class CapitalizeWorker implements RobotWorker{
	@Override
	public String getDescription() {
		return "Capitalizes the blip's content";
	}
	
	@Override
	public String getInstruction() {
		return "CAP";
	}
	
	/**
	 * No rocket science here, this worker just capitalizes the blip's content.
	 */
	@Override
	public boolean doWork(RobotMessageBundle bun, Blip bli, Event ev,
			String params) 
	{
		String txt = bli.getDocument().getText() ;
		txt = txt.toUpperCase();
		bli.getDocument().replace(txt);  
		return true; /* important so that potential following workers get executed. */
	}
}

class LengthWorker implements RobotWorker {
@Override
	public String getInstruction() {
		return "len";
	}

	@Override
	public String getDescription() {
		return "Displays the length of the blip in a child blip";
	}

	@Override
	public boolean doWork(RobotMessageBundle bun, Blip bli, Event eve,String params) 
	{	
		Blip lengthChild = bli.createChild();
		lengthChild.getDocument().append("length : " + bli.getDocument().getText().length() + " characters");
		return true;
	}

}
